<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring 异步处理机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 12px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.6s ease-out;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                Spring 异步处理机制
                <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析与实战指南</span>
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl">
                <i class="fas fa-quote-left mr-2"></i>
                掌握 Spring 的异步编程艺术，让你的应用性能飞跃提升
                <i class="fas fa-quote-right ml-2"></i>
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">S</span>pring 的异步处理机制依赖于 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解以及 Java 提供的线程池机制，通过灵活的配置实现异步任务的执行。这种机制不仅能够提升应用的响应速度，还能充分利用多核处理器的优势，让你的应用在高并发场景下游刃有余。
            </p>
        </div>

        <!-- Architecture Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap text-purple-600 mr-3"></i>异步处理架构图
            </h2>
            <div class="mermaid">
                graph TB
                    A[客户端请求] --> B[Spring Controller]
                    B --> C{是否异步方法?}
                    C -->|是| D[@Async 代理]
                    C -->|否| E[同步执行]
                    D --> F[TaskExecutor]
                    F --> G[线程池]
                    G --> H[异步任务执行]
                    H --> I[Future/CompletableFuture]
                    I --> J[返回结果]
                    E --> K[直接返回]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style D fill:#bbf,stroke:#333,stroke-width:2px
                    style F fill:#bfb,stroke:#333,stroke-width:2px
                    style G fill:#fbf,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Section 1 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">1</span>
                Spring 异步处理的概述
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 异步处理机制通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解提供了简便的异步任务执行方式。通过在方法上加上 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解，Spring 会自动将该方法的执行异步化，通常是将方法的执行交给一个配置好的线程池来处理，而不会阻塞调用线程。
            </p>
            
            <div class="bg-gray-50 rounded-lg p-4 mb-6">
                <p class="font-semibold text-gray-800 mb-3">
                    <i class="fas fa-code text-purple-600 mr-2"></i>关键源码：
                </p>
                <div class="code-block">
                    <pre><code>@Target(value=TYPE)
@Retention(value=RUNTIME)
@EnableAspectJAutoProxy(proxyTargetClass=false)
@Import(value=AsyncConfiguration.class)
@ComponentScan(value="org.springframework.scheduling.annotation")
public @interface EnableAsync {
}</code></pre>
                </div>
            </div>
            
            <div class="bg-purple-50 border-l-4 border-purple-500 p-4 rounded">
                <p class="text-gray-700">
                    <i class="fas fa-lightbulb text-purple-600 mr-2"></i>
                    <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@EnableAsync</code> 注解开启异步功能，通常在配置类中使用。这个注解的作用是启动 Spring 的异步功能，启用基于代理的异步执行。
                </p>
            </div>
        </div>

        <!-- Section 2 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">2</span>
                @Async 注解的使用
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 是 Spring 异步机制的核心注解，标记一个方法为异步方法。使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解的方法将会在后台线程池中执行，而不影响主线程的执行。
            </p>
            
            <div class="grid md:grid-cols-2 gap-6 mb-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-lg">
                    <div class="feature-icon">
                        <i class="fas fa-rocket"></i>
                    </div>
                    <h4 class="text-xl font-semibold mb-2">性能提升</h4>
                    <p class="text-gray-700">异步执行避免了线程阻塞，显著提升应用响应速度</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 p-6 rounded-lg">
                    <div class="feature-icon">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h4 class="text-xl font-semibold mb-2">资源优化</h4>
                    <p class="text-gray-700">充分利用线程池，合理分配系统资源</p>
                </div>
            </div>
            
            <div class="bg-gray-50 rounded-lg p-4 mb-6">
                <p class="font-semibold text-gray-800 mb-3">
                    <i class="fas fa-code text-purple-600 mr-2"></i>示例代码：
                </p>
                <div class="code-block">
                    <pre><code>@Service
public class MyService {

    @Async
    public void asyncMethod() {
        // 模拟耗时操作
        System.out.println("Start Async Task: " + Thread.currentThread().getName());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("End Async Task: " + Thread.currentThread().getName());
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Section 3 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">3</span>
                异步任务执行的代理机制
            </h3>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                Spring 的异步处理是基于代理模式实现的。Spring 会为带有 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解的方法创建一个代理对象，所有对该方法的调用都会通过代理来转发，代理会将方法的执行交给线程池来执行。
            </p>
            
            <div class="highlight-box">
                <h4 class="text-xl font-semibold mb-3">
                    <i class="fas fa-info-circle mr-2"></i>代理机制的核心优势
                </h4>
                <ul class="space-y-2">
                    <li><i class="fas fa-check mr-2"></i>透明性：调用者无需关心异步实现细节</li>
                    <li><i class="fas fa-check mr-2"></i>灵活性：可以动态切换同步/异步执行</li>
                    <li><i class="fas fa-check mr-2"></i>可扩展性：易于添加额外的横切关注点</li>
                </ul>
            </div>
        </div>

        <!-- Section 4 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8 animate-fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="section-number">4</span>
                TaskExecutor 的配置和执行
            